<?php
function sql_sel($table,$sel_field,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;
}

function sql_update_one($table, $up_field, $up_value, $wh_field, $wh_value, $xs = 0) { //更新某个字段的值，$xs表示更新的方式，0表示累加，1表示更新
	global $BIAOTOU;
	if ($xs == 0){
		$sql = "update " . $BIAOTOU . "$table set $up_field=$up_field+'$up_value' where $wh_field='$wh_value'";
	}
	if ($xs == 1){
		$sql = "update " . $BIAOTOU . "$table set $up_field='$up_value' where $wh_field='$wh_value'";
	}
	$query = mysql_query($sql);
	if ($query){
		return sql_sel($table, $up_field, $wh_field, $wh_value);
	}
	else{
		return false;
	}
}

function sql_update($table,$filed_arr,$wh_field,$wh_value){
    global $BIAOTOU;
	$sql="";
	foreach($filed_arr as $k=>$v){
		$sql="`$k`='$v',".$sql;
		}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where `$wh_field`=$wh_value;";
	mysql_query($sql);
}

function sql_insert_one($table,$filed_arr){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($filed_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	$query=mysql_query($sql);
	if($query) return 1;
	else return mysql_error();
}

function inc_fanli($dduserid,$fxje,$tgje,$order_code){
	$dduser=sql_sel("user", "ddusername,dengji", "Id", $dduserid);//求会员名，等级
	$ddusername=$dduser['ddusername'];
    sql_update_one("user", "money", $fxje, "Id", $dduserid, 0);//增加会员金额
	sql_update_one("user", "dengji", 1, "Id", $dduserid, 0);//增加会员等级
	
	
	//用户消息
	$title='您获得了新的交易返现！';
	$trade_id=$order_code;
	$msg_tabao="您获得了新的交易返现，商城订单号".$order_code."返现金额".$fxje;
	$filed_arr=array('title'=>$title,'content'=>$msg_tabao,'addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$ddusername,'senduser'=>'网站客服');
	sql_insert_one("msg",$filed_arr);
	
	//用户明细
	$shijian="交易返现";
	$memo="交易号$order_code";
	$filed_arr=array('ddusername'=>$ddusername,'shijian'=>$shijian,'addtime'=>date('Y-m-d H:i:s'),'je'=>$fxje,'memo'=>$memo);
	sql_insert_one("mingxi",$filed_arr);
	
	//求推荐人
	$tjrid=sql_sel("user", "tjr", "ddusername", $ddusername);
	if($tjrid>0){
		$tjrname=sql_sel("user", "ddusername", "Id", $tjrid);
	    //增加推荐人佣金
		sql_update_one("user", "money", $tgje, "Id", $tjrid, 0);
					
		//用户消息
		$title='您获得了新的推广佣金！';
		$msg_taobaotuiguang="您获得了新的推广佣金".$tgje;
		$filed_arr=array('title'=>$title,'content'=>$msg_taobaotuiguang,'addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$tjrname,'senduser'=>'网站客服');
		sql_insert_one("msg",$filed_arr);
					
		//用户明细
		$shijian="推广佣金";
		$memo="交易人$ddusername";
		$filed_arr=array('ddusername'=>$tjrname,'shijian'=>$shijian,'addtime'=>date('Y-m-d H:i:s'),'je'=>$tgje,'memo'=>$memo);
		sql_insert_one("mingxi",$filed_arr);
	}
}

function get_object_vars_final($obj) {
	if (is_object($obj)) {
		$obj = get_object_vars($obj);
	}

	if (is_array($obj)) {
		$n=0;
		foreach ($obj as $key => $value) {
			if(($key=='bill_yyyymmdd' or $key=='u_id') and $n>0){
				if(is_object($value)) $value='';
			}
			$obj[$key] = get_object_vars_final($value);
			$n++;
		}
	}
	return $obj;
}

$yyyymmdd=date('Ymd');//日期
/*$m_id=$_GET['m_id'];//广告主ID*/
$u_id=$_SESSION['dduserid'];//会员ID
$mm = md5("linktech^" . MALLMM);
if($u_id>0){ 
    $linktech = "http://www.linktech.cn/AC/trans_list.htm?account_id=" . MALLZH . "&sign=" . $mm . "&syyyymmdd=" . $yyyymmdd . "&eyyyymmdd=" . $yyyymmdd . "&affiliate_id=".MALLWZBH."&output_type=json&type=cps&u_id=".$u_id;
    $content=file_get_contents($linktech);
	$xmlCode = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
    $arrdata=get_object_vars_final($xmlCode);
	$result=$arrdata['is_success']['@attributes']['result'];
    $count=$arrdata['order_list']['@attributes']['count'];
    $arr_data=$arrdata['order_list']['order'];
	$i=0; //记录新订单
	$j=0; //记录更新订单
    foreach($arr_data as $order){
		$order_time = $order['order_time'];//下单时间
		$merchant_id = $order['merchant_id'];//商城联盟ID
		$u_id = $order['u_id'];//会员ID

		$dduser=sql_sel("user", "ddusername,dengji", "Id", $u_id);
		$ddusername=$dduser['ddusername'];
		$dengji=$dduser['dengji'];
		
		$order_code = $order['order_code'];//订货号
		$product_code = $order['product_code'];//商品号，在购物车中，商品的订货号相同，用商品号区别
		$item_price = $order['item_price'];//单价
		$item_count = $order['item_count'];//数量
		$sales = $order['sales'];//总额
		$commission = $order['commission'];//返利金额
		$stat_desc = $order['stat_desc'];//订单状态
		$bill_yyyymmdd = $order['bill_yyyymmdd'];//结算日
		$fxje = ck_fxje($commission,$dengji);  //会员返利金额
		$tgje = round($commission * TGBL, 2);  //给会员的推广佣金
		//添加交易记录
		$id=sql_sel("mall_order", "id", "order_code", $order_code," and product_code='".$product_code."'");//用订货号和商品编号验证该交易是否存在于数据库
		if($id==''){//交易不存在
			
		    $filed_arr=array('order_time'=>$order_time,'product_code'=>$product_code,'merchant_id'=>$merchant_id,'u_id'=>$u_id,'order_code'=>$order_code,'item_count'=>$item_count,
							 'item_price'=>$item_price,'sales'=>$sales,'commission'=>$commission,'stat_desc'=>$stat_desc,'bill_yyyymmdd'=>$bill_yyyymmdd,
							 'fxje'=>$fxje,'ddusername'=>$ddusername);
			sql_insert_one("mall_order",$filed_arr);
			$i=$i+1;
			if($stat_desc=="核对有效" and $u_id!=''){
			    inc_fanli($u_id,$fxje,$tgje,$order_code,$merchant_id);
			}
		}
		else{
			$table_stat_desc=sql_sel("mall_order", "stat_desc", "id", $id); //订单存在，求此订单的状态
		    if($table_stat_desc!=$stat_desc){
				$filed_arr=array('stat_desc'=>$stat_desc,'bill_yyyymmdd'=>$bill_yyyymmdd);
			    sql_update("mall_order",$filed_arr,"id",$id); //更新订单状态
				if($u_id!='' and $stat_desc=='核对有效'){
				    inc_fanli($u_id,$fxje,$tgje,$order_code,$merchant_id);
					++$j;
				}
			}
		}
	}
	if ($i != 0) $msgi = "导入订单".$i."条";
    if ($j != 0) $msgj = "更新订单".$j."条";
    if($i==0 and $j==0) $msg="无订单";
}
?>